<template>
  <div class="app-container">
    <el-row style="margin-bottom: 10px">
      <el-col :span="4">
        <div class="grid-content bg-purple">
          <span class="grid">省</span>
          &nbsp; &nbsp;
          <el-select @change="test" v-model="value7" placeholder="请选择状态">
            <el-option
              v-for="(item, index) in provinceList"
              :key="index"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
          &nbsp;&nbsp;
          <div class="grid-content bg-purple">
            <span class="grid">市</span>
            &nbsp; &nbsp;
            <el-select
              @change="test2"
              v-model="params.code"
              placeholder="请选择状态"
            >
              <el-option
                v-for="(item, index) in selectCityList"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple">
          <span class="grid">时间</span>
          &nbsp; &nbsp;
          <el-date-picker
            v-model="value6"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            @change="change"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions2"
          >
          </el-date-picker>
        </div
        >
      </el-col>

      <el-col :span="1">
        <el-button @click="fetchData" type="primary" icon="el-icon-search">查询</el-button>
      </el-col>
      <el-col  :span="1" style="margin-left: 35px">
        <el-button style="color: #FF7968" @click="clear" icon="el-icon-refresh" plain>重置</el-button>
      </el-col>
      <el-col :span="1" style="margin-left: 30px">

        <el-button class="add" type="primary" @click="toProduct" icon="el-icon-plus">新增</el-button>
      </el-col>
    </el-row>

    <br/>
    <el-table
      v-loading="listLoading"
      :data="list"
      show-header
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="商品ID" width="50">
        <template slot-scope="scope">
          <!-- {{ scope.$index }} -->
          {{ scope.row.productId }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称">
        <template slot-scope="scope">
          {{ scope.row.productName }}
        </template>
      </el-table-column>
      <el-table-column label="预售票价格">
        <template slot-scope="scope">
          {{ scope.row.sellPrice }}
        </template>
      </el-table-column>
      <el-table-column label="现场票价格">
        <template slot-scope="scope">
          {{ scope.row.sitePrice }}
        </template>
      </el-table-column>
      <el-table-column label="起始时间">
        <template slot-scope="scope">
          {{ scope.row.startTime }} --{{ scope.row.endTime }}
        </template>
      </el-table-column>
      <el-table-column label="库存">
        <template slot-scope="scope">
          {{ scope.row.stock }}
        </template>
      </el-table-column>
      <el-table-column label="地址">
        <template slot-scope="scope">
          {{ scope.row.address }}
        </template>
      </el-table-column>
      <el-table-column label="种类">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.typeId!=0" type="success">漫展</el-tag>
          <el-tag v-if="scope.row.typeId==0" >周边</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图片" width="110" align="center">
        <template slot-scope="scope">
          <img
            style="width: 40px; height: 40px; border-radius: 3px"
            :src="scope.row.skuImg"
            alt=""
          />

          <!-- <span>{{ scope.row.author }}</span> -->
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <i style="color: #409eff" class="el-icon-edit"></i>
          <el-button @click="edit(scope.row.productId)" type="text"
          >修改
          </el-button
          >
          <i style="color: #409eff" class="el-icon-delete"></i>
          <el-button
            @click="open2(scope.row.productId)"
            slot="reference"
            type="text"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {deleteUserById} from "@/api/user";
  import {listProvinces} from "@/api/dict";
  import {listCity} from "@/api/dict";
  import {listProduct} from "@/api/product";
  import {deleteProductById} from "@/api/product";

  export default {
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: "success",
          draft: "gray",
          deleted: "danger",
        };
        return statusMap[status];
      },
    },
    data() {
      return {
        list: null,
        total: 0,
        listLoading: true,
        provinceList: [],
        cityList: [],
        selectCityList: [], // 筛选
        params: {
          pageNum: 1,
          limit: 10,
          code: "",
          provinceId: 0,
          cityId: 0,
          date: [],
        },
        ProvinceCode: "",
        pickerOptions2: {
          shortcuts: [
            {
              text: "最近一周",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", [start, end]);
              },
            },
            {
              text: "最近一个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit("pick", [start, end]);
              },
            },
            {
              text: "最近三个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit("pick", [start, end]);
              },
            },
          ],
        },
        value6: "",
        value7: "",
      };
    },
    created() {
      this.fetchData();
      this.getProvinceList();
      this.getCityList();
    },
    methods: {
      toProduct() {
        this.$router.push('/product/save')
      },
      test2(val) {
        this.params.cityId = val;
      },
      test(val) {
        let provinceList = this.provinceList;
        //  获取省份id
        for (let i = 0; i < provinceList.length; ++i) {
          if (val == provinceList[i].code) {
            this.params.provinceId = provinceList[i].id;
          }
        }
        this.ProvinceCode = val;
        let cityList = this.cityList;
        if (this.selectCityList.length != 0) {
          this.selectCityList = [];
        }
        for (let i = 0; i < cityList.length; i++) {
          if (cityList[i].provinceCode == this.ProvinceCode) {
            this.selectCityList.push(cityList[i]);
          }
        }
      },
      getCityList() {
        listCity()
          .then((res) => {
            this.cityList = res.data;
          })
          .catch((reason) => {
          });
      },
      getProvinceList() {
        listProvinces().then((res) => {
          this.provinceList = res.data;
        });
      },
      clear() {
        this.params = {};
        this.params.pageNum = 1;
        this.params.limit = 6;
        this.params.nickname = "";
        this.params.date = [];
        this.params.status = 0;
        this.fetchData();
      },
      change(val) {
        this.params.date = this.value6;
      },
      handleSizeChange(val) {
        this.params.limit = val;
        this.fetchData();
      },
      handleCurrentChange(val) {
        this.params.pageNum = val;
        this.fetchData();
      },
      fetchData() {
        this.listLoading = true;

        listProduct(this.params).then((response) => {
          console.log(response);
          this.list = response.data.ProductList;
          this.total = response.data.total;
          this.listLoading = false;
        });
      },
      open2(productId) {
        this.$confirm("是否删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deleteProductById(productId)
              .then((res) => {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.fetchData();
              })
              .catch((reason) => {
                this.$message({
                  type: "error",
                  message: resason,
                });
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      edit(productId) {
        this.$router.push({
          path: "/product/edit",
          query: {
            productId: productId,
          },
        });
      },
    },
  };
</script>

<style scoped>
  .add {
    background-color: #E7FAF0 !important;
    color: #13CE66 !important;
    border: 1px solid #E7FAF0 !important;
  }

  .add:hover {
    border: 1px solid #13CE66 !important;
    background-color: #13CE66 !important;
    color: #fff !important;

  }

  .grid-content {
    display: flex;
    align-items: center;
    justify-content: center;
    /* jus */
  }

  .grid {
    font-weight: 600;
    color: #606266;
  }

  /deep/ .has-gutter th {
    background-color: #f8f8f9;
  }
</style>>

